
package org.BattleOfGoode.model;

//import org.BattleOfGoode.model.components.Speed;

import org.BattleOfGoode.droidz.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;

/**
 * This is a test droid that is dragged, dropped, moved, smashed against the
 * wall and done other terrible things with. Wait till it gets a weapon!
 * 
 * @author impaler
 * 
 */
public class Goode {

	protected Bitmap bitmap;
	private int x, y;
	private PointF speed;
	private int width;
	private int height;
	private boolean touched;
	private int radius;

	public Goode(int radius, int x, int y, Bitmap imagem) {		
		this.bitmap = imagem;
		this.radius = radius;
		this.x = x;
		this.y = y;
		speed = new PointF(0,0);
	}

	// SETS
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setRadius(int value) {
		this.radius = value;
		
	}

	public void setTouched(boolean touched) {
		this.touched = touched;
	}

	public void setSpeed(PointF vel) {
		this.speed = vel;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	// GETS
	public int getHeight() {
		return this.height;
	}

	public int getWidth() {
		return this.width;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public int getX() {
		return x;
	}

	public Point getPos() {
		Point pos = new Point();
		pos.x = this.x;
		pos.y = this.y;

		return pos;
	}

	public int getY() {
		return this.y;
	}

	public PointF getSpeed() {
		return speed;
	}

	public int getRadius() {
		return radius;
	}

	public boolean isTouched() {
		return touched;
	}


	// METODOS
	public void draw(Canvas canvas) {	
		canvas.drawBitmap(bitmap,
				null, new Rect(x - this.getRadius(), y - this.getRadius(), 
				x + this.getRadius(), y + this.getRadius()), null);
				//, x - (bitmap.getWidth() / 2),
				//y - (bitmap.getHeight() / 2), null);
	}

	public void bounceBack() {
		PointF s = this.getSpeed();
		s.x = -s.x;
		s.y = -s.y;
		this.setSpeed(s);
		this.move();
	}

	public void handleActionDown(int eventX, int eventY) {
		if (eventX >= (x - bitmap.getWidth() / 2)
				&& (eventX <= (x + bitmap.getWidth() / 2))) {
			if (eventY >= (y - bitmap.getHeight() / 2)
					&& (y <= (y + bitmap.getHeight() / 2))) {
				// droid touched
				setTouched(true);
			} else {
				setTouched(false);
			}
		} else {
			setTouched(false);
		}

	}

	public boolean bounce(int width, int height) {
		PointF speed = this.getSpeed();
		boolean x0 = (Math.abs(this.getX()) <= this.getRadius()) && speed.x < 0;
		boolean y0 = (Math.abs(this.getY()) <= this.getRadius()) && speed.y < 0;
		boolean x1 = (Math.abs(this.getX() - width) <= this.getRadius()) && speed.x > 0;
		boolean y1 = (Math.abs(this.getY() - height) <= this.getRadius()) && speed.y > 0;
		
		// check collision with right wall if heading right
		if (x0 || x1) {
			speed.x *= -1;
		}
		// check collision with bottom wall if heading down
		if (y0 || y1) {
			speed.y *= -1;
		}
		
		this.setSpeed(speed);
		this.move();
		return false;
	}
	
	
	public boolean collision(int x, int y) {
		boolean cX = Math.abs(x - this.getX()) <= this.getRadius();
		boolean cY = Math.abs(y - this.getY()) <= this.getRadius();

		return (cX && cY);
	}	

	public boolean collision(Goode el) {
		double sq_distance = Math.pow((double)(el.getX() - this.getX()),2.0) + Math.pow((double)(el.getY() - this.getY()),2);
		double sq_radius_sum = Math.pow(this.radius + el.radius, 2);
//		boolean x = Math.abs(el.getX() - this.getX()) <= this.getRadius();
//		boolean y = Math.abs(el.getY() - this.getY()) <= this.getRadius();

		return (sq_distance < sq_radius_sum);//x && y);
	}

	public void disappear() {
		this.setRadius(0);
	}

	public void grow(int radius) {
		this.setRadius(this.radius + radius);
	}
	
	public void move(){
		this.x += speed.x;
		this.y += speed.y;
	}
}
